package com.example.mediaplay;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import com.tbruyelle.rxpermissions3.RxPermissions;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import io.reactivex.rxjava3.functions.Consumer;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;

public class MainActivity extends AppCompatActivity {
    private ImageButton button;
    private MediaPlayer mediaPlayer;
    private ImageButton button1;
    private SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RxPermissions rxPermissions = new RxPermissions(this);

        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Throwable {
                if (aBoolean){
                    intiView();
                }

            }
        });

    }
    private void intiView(){
        button=findViewById(R.id.start);
        button1=findViewById(R.id.pause);
        seekBar=findViewById(R.id.seekBar_but);
        button1.setEnabled(false);
        intiMedia();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果点击停止后再次点击按钮  需要初始化MediaPlayer
                if(null==mediaPlayer){
                    intiMedia();
                }
                //根据当前播放状态，设置播放按钮图片
                button.setImageResource(mediaPlayer.isPlaying()?R.drawable.ic_baseline_play_arrow_24:R.drawable.ic_baseline_pause_24);
                if (mediaPlayer.isPlaying()){
                    pauseMusic();
                }else{
                    playMusic();
                }
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopMusic();
            }
        });
        //拖动到具体时间
        mediaPlayer.seekTo(2500);
        //获取媒体总时长
        mediaPlayer.getDuration();
        //拖拽进度条的进度监听
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    private void intiMedia(){
       mediaPlayer=new MediaPlayer();
       button1.setEnabled(true);
        try {//MediaPlayer 初始化
            File musicFile=getLocalMusic();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(musicFile.getAbsolutePath());
            mediaPlayer.prepare();
            //设置当前媒体循环播放
            //mediaPlayer.setLooping(true);

            //监听当前是否播放完成
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {

                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void playMusic(){
        if (!mediaPlayer.isPlaying()){
            mediaPlayer.start();
        }
    }
    private void pauseMusic(){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }
    }
    private  void stopMusic(){
        button1.setEnabled(false);
        button.setImageResource(R.drawable.ic_baseline_play_arrow_24);
        mediaPlayer.stop();
        mediaPlayer.reset();
        mediaPlayer.release();
        mediaPlayer=null;
    }
    private File getLocalMusic() throws IOException {
        String musicPath=null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            String rootPath=Environment.getExternalStorageDirectory().getCanonicalPath();
            File musicDir=new File(rootPath+"/Music");
            //获取外部存储中Music目录下的所有.mp3文件
            File[] musicFile=musicDir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(".mp3");
                }
            });
            if(null==musicFile){
                return null;
            }
            return musicFile[0];
        }
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopMusic();
    }
}
